/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isgesis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Agustin
 */
public class PersistenciaTelefono {

    private static PersistenciaTelefono instance;

    public static PersistenciaTelefono getInstance() {
        if (instance == null) {
            instance = new PersistenciaTelefono();
        }
        return instance;
    }

    public ResultSet ejecutarSeleccion(String query) throws SQLException {
        ResultSet rs;
        try {
            Conexion.getInstance().conectar();
            rs = Conexion.getInstance().select(query);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
        return rs;
    }

    public Telefono recorerRS(ResultSet rs) throws SQLException, ParseException {
        Telefono t1 = new Telefono();
        try {
            t1.setId(Integer.parseInt(rs.getObject(1).toString()));
            t1.setTelefono(rs.getObject(2).toString());
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
        return t1;
    }

    public List<Telefono> traerRS(String query) throws ParseException {
        List<Telefono> telefonos = new ArrayList<Telefono>();
        try {
            ResultSet rs = ejecutarSeleccion(query);
            if (rs.next()) {
                telefonos.add(recorerRS(rs));
                while (rs.next()) {
                    telefonos.add(recorerRS(rs));
                }
            } else {
                Telefono t = new Telefono();
                telefonos.add(t);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        return telefonos;
    }

    public List<Telefono> selectTelefonos(ArrayList<Object> datos) throws ParseException, SQLException {
        String query = "";
        if (datos.get(1) == "idCliente") {
            query = "select *from telefonos where idTelefono in (SELECT idTelefono FROM clientestelefonos WHERE idCliente=" + datos.get(0) + ") ";
        } else {
            query = "select *from telefonos where idTelefono in (SELECT idTelefono FROM lugarestelefonos WHERE idLugar=" + datos.get(0) + ")";
        }
        return traerRS(query);
    }

    public int obtenerUltimoID() throws SQLException {
        int id;
        try {
            String query = "select max(idTelefono) from telefonos";
            Conexion.getInstance().conectar();
            ResultSet rs = ejecutarSeleccion(query);
            if (rs.next()) {
                id = Integer.parseInt(rs.getObject(1).toString());
            } else {
                id = 0;
            }
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
        return id;
    }

    public void insert(Telefono telefono, Integer idCliente, Integer idLugar, Integer idFuncionario) throws ParseException {
        String query = "";
        try {
            query = "INSERT INTO telefonos (numero) VALUES('" + telefono.getTelefono() + "')";
            if (idLugar.equals(null)) {
                query = "INSERT INTO lugarestelefonos (idLugar, idTelefono) VALUES('" + idLugar + "'," + obtenerUltimoID() + ")";
            } else if (idCliente.equals(null)) {
                query = "INSERT INTO clientestelefonos (idCliente, idTelefono) VALUES('" + idCliente + "'," + obtenerUltimoID() + ")";
            } else {
                query = "INSERT INTO funcionariostelefonos (idFuncionario,idTelefono) VALUES('" + idFuncionario + "'," + obtenerUltimoID() + ")";
            }
            Conexion.getInstance().conectar();
            Conexion.getInstance().insert(query);
        } catch (SQLException ex) {
            Logger.getLogger(isgesis.PersistenciaEstado.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
